package cmsapi

import (
	// "net/http"
	"encoding/json"
	"strconv"
	// "time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	
	"oxy_server/module/log"
	"oxy_server/model"
	"oxy_server/until"
)

type (
	designgroup struct {
		ID bson.ObjectId `json:"_id" bson:"_id"`
		ProductID 					string `json:"productid" bson:"productid"`
		ProductNumberStyleName		string `json:"productnumberstylename" bson:"productnumberstylename"`
		Color 						string `json:"color" bson:"color"`
		Material					string `json:"material" bson:"material"`
		Title						string `json:"title" bson:"title"`
		Content						string `json:"content" bson:"content"`
		UnitPrice					string `json:"unitprice" bson:"unitprice"`
		TotalPrice					string `json:"totalprice" bson:"totalprice"`
		ProcessTubePinCost			string `json:"processtubepincost" bson:"processtubepincost"`
		Cost 						string `json:"cost" bson:"cost"`
		ReferenceCard 				string `json:"referencecard" bson:"referencecard"`
		Features 					string `json:"features" bson:"features"`
		DesignAndTechnology 		string `json:"designandtechnology" bson:"designandtechnology"`
		ModelType		 			string `json:"modeltype" bson:"modeltype"`
		Note		 				string `json:"note" bson:"note"`
		Status						string `json:"status" bson:"status"`
		// SaleCost					string `json:"salecost" bson:"salecost"`
		// MaterialGroup				string `json:"materialgroup" bson:"materialgroup"`
		Clothversion				string `json:"clothversion" bson:"clothversion"`
	}
	insertdesigngroup struct {
		ProductID 					string `json:"productid" bson:"productid"`
		ProductNumberStyleName		string `json:"productnumberstylename" bson:"productnumberstylename"`
		Color 						string `json:"color" bson:"color"`
		Material					string `json:"material" bson:"material"`
		Title						string `json:"title" bson:"title"`
		Content						string `json:"content" bson:"content"`
		UnitPrice					string `json:"unitprice" bson:"unitprice"`
		TotalPrice					string `json:"totalprice" bson:"totalprice"`
		ProcessTubePinCost			string `json:"processtubepincost" bson:"processtubepincost"`
		Cost 						string `json:"cost" bson:"cost"`
		ReferenceCard 				string `json:"referencecard" bson:"referencecard"`
		Features 					string `json:"features" bson:"features"`
		DesignAndTechnology 		string `json:"designandtechnology" bson:"designandtechnology"`
		ModelType		 			string `json:"modeltype" bson:"modeltype"`
		Note		 				string `json:"note" bson:"note"`
		Status						string `json:"status" bson:"status"`
		// SaleCost					string `json:"salecost" bson:"salecost"`
		// MaterialGroup				string `json:"materialgroup" bson:"materialgroup"`
		Clothversion				string `json:"clothversion" bson:"clothversion"`
		
	}
)

func GetDesignGroupListHandler(c echo.Context) error {
	currentpage,err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage,err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="design_group"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	query,count,err:=model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]designgroup{}
	query.All(&ps)
	
	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":string(str),
		"count":count,
	})

	return nil
}

func AddDesignGroupHandler(c echo.Context) error {
	d:= &insertdesigngroup{}
	if err := c.Bind(d); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	model.DB().Dbname="OxyCms"
	model.DB().Document="design_group"
	err:=model.DB().Insert(d)
	if err != nil {
		log.Error("插入出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
	})

	return nil	
}
func UpdateDesignGroupHandler(c echo.Context) error {
	id := c.QueryParam("id") 
	log.Info("UpdateDesignGroupHandler",id)
	
	d:= &insertdesigngroup{}
	if err := c.Bind(d); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	
	model.DB().Dbname="OxyCms"
	model.DB().Document="design_group"
	err:= model.DB().Update(bson.M{"_id":bson.ObjectIdHex(id)} , d)
	if err != nil{
		log.Error("更新出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	model.DB().Document="products"
	selector := bson.M{"_id": bson.ObjectIdHex(d.ProductID)}
	data := bson.M{"$set": bson.M{"designteam": d.Status}}
	err = model.DB().Update(selector, data)
	if err != nil{
		log.Error("更新出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}
func DelDesignGroupHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelDesignGroupHandler",id)
	model.DB().Dbname="OxyCms"
	model.DB().Document="design_group"
	err:=model.DB().Remove(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		log.Error("删除出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":  "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}